package droid.ru;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;


public class RuDroidActivity extends Activity {
	
	private int selectedDay = 0;
	private Date today;
	private boolean gotData = false; 
	// ListView for the menu items
	private ListView myListView;
	// ArrayList containing all week days menus
	private ArrayList<ArrayList<String>> menu;
	// ArrayList containing the selected week day menu
	private ArrayList<String> dayMenu;
	// ArrayAdpater to populate ListView
	private ArrayAdapter<String> aa;
	
	private Button nextButton, previousButton;
	private TextView dayTextView;
	
	private DbHelper dbHelper;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);				
		getMenus();		
		assingControls();
		setDay();	
		populateListView(selectedDay);
	}

	private void getMenus()
	{
		try {
			
			menu = MenuHelper.fetchWeekData();
			/*boolean downloadData = false;
			dbHelper.createDataBase();
			dbHelper.openDataBase();
			if(dbHelper.menuUpToDate())
			{
				gotData = true;
				//menu = dbHelper.getMenu();				
			}
			else
			{
				// TODO ask user for permission to use his internet connection
				menu = MenuHelper.fetchWeekData();
				if(menu.size() == 5) {
					gotData = true;
				}					
				//dbHelper.saveMenu(menu);
			}*/
			
			/*
			 * 1 Check last data refresh date.
			 * 1.1 > If empty, download data, save to db and show
			 * 1.2 > Else, check if still valid
			 * 1.2.1 > If yes, load from db and show
			 * 1.2.2 > Else do step 1.1
			 */
			
			
		} 
		catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private void setDay()
	{		
		try {
			today = Calendar.getInstance().getTime();
			selectedDay = DateUtils.getWeekDay();			
			
			if(selectedDay == 0) {
				previousButton.setEnabled(false);
			}			
			else if (selectedDay == 4) {
				nextButton.setEnabled(false);
			}
			
			updateDayLabel();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	public void updateDayLabel()
	{
		switch(selectedDay)
		{
			case 0:
				dayTextView.setText("segunda-feira");
				break;
			case 1:
				dayTextView.setText("terca-feira");
				break;
			case 2:
				dayTextView.setText("quarta-feira");
				break;
			case 3:
				dayTextView.setText("quinta-feira");
				break;
			case 4:
				dayTextView.setText("sexta-feira");
				break;
		}
	}
	
	// Link our Button's to the inflate XML correct id's and assign their callback's 
	private void assingControls()
	{		
		try {
			dayTextView = (TextView)findViewById(R.id.dayTextView);
			
			nextButton = (Button)findViewById(R.id.buttonNext);
			nextButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					gotoNextDay();
				}			
			});
			
			previousButton = (Button)findViewById(R.id.buttonPrevious);
			previousButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					gotoPreviousDay();
					
				}
			});
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void gotoNextDay() {
		try {
			// TODO Auto-generated method stub
			if(selectedDay == 3)
			{			
				nextButton.setEnabled(false);
			}
			else if (selectedDay == 0)
			{
				previousButton.setEnabled(true);
			}
			selectedDay++;
			updateDayLabel();
			populateListView(selectedDay);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void gotoPreviousDay() {
		try {
			// TODO Auto-generated method stub
			if(selectedDay == 1)
			{
				previousButton.setEnabled(false);
			}
			else if (selectedDay == 4)
			{
				nextButton.setEnabled(true);
			}
			selectedDay--;
			updateDayLabel();
			populateListView(selectedDay);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void populateListView(int day) {
		try {
			dayMenu = menu.get(day);
			aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dayMenu);	
			myListView = (ListView)findViewById(R.id.myListView);
			myListView.setAdapter(aa);
			
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.app_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.about:
	        showAbout();
	        return true;
	    case R.id.settings:
	        showSettings();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}

	private void showSettings() {
		Intent intent = new Intent(this, SettingsActivity.class);
		startActivity(intent);
	}

	private void showAbout() {
		Intent intent = new Intent(this, AboutActivity.class);
		startActivity(intent);
	}
	
	
}